﻿namespace EyeOpen.TimeAssistant.Service
{
    using EyeOpen.Domain;
    using EyeOpen.TimeAssistant.Domain;
    using EyeOpen.TimeAssistant.Server.BootStrapper;
    using EyeOpen.Validation;

    public class TimeAssistantServiceContext
    {
        public void SaveUserStory(User user, UserStory userStory)
        {
            var userStoryValidator = new UserStoryValidator();

            if (!userStoryValidator.IsValid(userStory))
            {
                throw new ValidationException(userStoryValidator.ValidationResults);
            }

            using (var timeAssistantDataContext = new TimeAssistantDataContextFactory().CreateDataContext())
            {
                timeAssistantDataContext.ExecuteInTransaction(
                    () =>
                        {
                            timeAssistantDataContext.Add(user);
                            timeAssistantDataContext.Add(userStory.Meta);
                            timeAssistantDataContext.Add(userStory);
                        });
            }
        }
    }
}